/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package restService;

import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import net.elbandi.pve2api.Pve2Api;
import net.elbandi.pve2api.data.VmOpenvz;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * REST Web Service
 *
 * @author EleygiTheWane
 */
@Path("updateVM")
public class UpdateVMResource {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of UpdateVMResource
     */
    public UpdateVMResource() {
    }

    /**
     * Retrieves representation of an instance of net.elbandi.pve2api.UpdateVMResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    public String getJson() {
        //TODO return proper representation object
        throw new UnsupportedOperationException();
    }

    /**
     * PUT method for updating or creating an instance of UpdateVMResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) throws JSONException {
        
        JSONObject request = new JSONObject(content);
        System.out.println("Ce qu'on a recu en string "+ content);
        boolean isExists = false;
        
        Pve2Api api = new Pve2Api("192.168.100.55", "root", "pam", "passpass");
        
        //Mise a jour d'une VM
        try {
            api.login();
            //api.startOpenvz("proxmox", 105);
            //api.stopOpenvz("proxmox", 100, null);
            //api.shutdownOpenvz("proxmox", 100);
            //api.deleteQemu("proxmox", 100);
            
            System.out.println("login ok");
            JSONObject data = new JSONObject();
            
            //Teste si le vmid existe
            List<VmOpenvz> existingVM = api.getOpenvzCTs("proxmox");
            for(int i=0; i<existingVM.size(); i++){
                if(existingVM.get(i).getVmid() == request.optInt("vmid"))
                    isExists = true;
            }
            if(isExists=true){
                System.out.println("La vmid "+ request.optInt("vmid")+" existe");

                //valeurs a mettre a 0
                data.put("memory", 0);
                data.put("swap", 0);
                data.put("disk", 0);
                data.put("cpus", 0);

                data.put("cpu", 0);
                data.put("diskwrite", 0);
                data.put("maxdisk",0);
                data.put("maxmem", 0);
                data.put("maxswap", 0);
                data.put("mem",0);
                data.put("netin", 0);
                data.put("netout", 0);
                data.put("nproc", 0);
                data.put("status","");
                data.put("uptime", 0);
                data.put("cpuunits", 0);
                data.put("digest","");
                data.put("diskspace", 0);
                data.put("hostname","");
                data.put("nameserver","");
                data.put("netif","");
                data.put("onboot", true);
                data.put("quotatime", 0);
                data.put("quotaugidlimit", 0);
                data.put("searchdomain", "");
                data.put("diskread", 0);
                data.put("name","");
                data.put("storage", "");
                data.put("ostemplate","");
                    //debian-7.0-standard_7.0-2_i386.tar.gz
                    //ubuntu-12.04-standard_12.04-1_i386.tar.gz
                data.put("memory", 0);
                data.put("ip_address","");


                //Date we (optionnaly) receive from the SAAS (the vmid is mandatory)
                data.put("vmid", request.optInt("vmid"));
                data.put("memory", request.optInt("memory"));
                data.put("swap", request.optLong("swap"));
                data.put("disk", request.optInt("disk"));
                data.put("cpus", request.optInt("cpus"));

                VmOpenvz vmOpenvz = new VmOpenvz(data); 
                vmOpenvz.SetConfig(data);
                api.updateOpenvz("proxmox", vmOpenvz);
            }
            else{
                System.out.println("La vmid "+request.optInt("vmid")+" n'existe pas");
            }           
        } catch(Exception e){
        e.printStackTrace();
        }
    }
}
